
#include <gfx/idevice.h>
#include <gfx/iprogram.h>
#include <gfx/iprogramparameter.h>
#include <gfx/programparametername.h>
#include <gfx/gl20/mesh/skinmeshgpu.h>
#include <scene/skeleton.h>
#include <assert.h>

ceSkinMeshGPUGL20::ceSkinMeshGPUGL20()
	: iSkinMesh ()
	, _skeleton (0)
  , _mesh(0)
{
  CE_OBJECT_CONSTR
}

ceSkinMeshGPUGL20::~ceSkinMeshGPUGL20()
{
  CE_UNSET(_mesh);
}

void ceSkinMeshGPUGL20::Draw(const ceRenderEnvironment&, iDevice* device)
{
  static ceProgramParameterName bonesParameterName ("SKELETON_BONES");

  iProgram *program = device->GetProgram();
  assert (program);

  iProgramParameter* param = program->GetParameter (bonesParameterName);
	assert (param);
	param->Bind (_skeleton->GetNumberOfBones (), _skeleton->GetBoneMatrices ());

  device->SetStreamSource(0, _mesh->GetVertices());
  device->SetIndices(_mesh->GetIndices());
  device->SetVertexDeclaration(_mesh->GetVertexDeclration());
  device->DrawIndexPrimitives(PT_Triangles, _mesh->GetIndexDataType(), _mesh->GetNumberOfPrims(), true);
}

void ceSkinMeshGPUGL20::Update(const ceRenderEnvironment&, iDevice*)
{

}

void ceSkinMeshGPUGL20::SetMesh (ceMesh* mesh)
{
  CE_SET(_mesh, mesh);
}

const ceMesh* ceSkinMeshGPUGL20::GetMesh () const
{
	return _mesh;
}

ceMesh* ceSkinMeshGPUGL20::GetMesh ()
{
	return _mesh;
}

void ceSkinMeshGPUGL20::SetSkeleton(ceSkeleton* skeleton)
{
  CE_SET(_skeleton, skeleton);
}

ceSkeleton* ceSkinMeshGPUGL20::GetSkeleton ()
{
	return _skeleton;
}

const ceSkeleton* ceSkinMeshGPUGL20::GetSkeleton () const
{
	return _skeleton;
}


const ceBoundingBox& ceSkinMeshGPUGL20::GetBoundingBox () const
{
  return _mesh->GetBoundingBox();
}
